package com.nbsaas.boot.controller.order;

import com.nbsaas.boot.order.ext.apis.OrderExtApi;
import com.nbsaas.boot.order.ext.domain.request.OrderExtRequest;
import com.nbsaas.boot.rest.annotations.AddOperator;
import com.nbsaas.boot.rest.annotations.DeleteOperator;
import com.nbsaas.boot.rest.annotations.UpdateOperator;
import com.nbsaas.boot.rest.annotations.ViewOperator;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.PageResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.rest.annotations.CreateData;
import com.nbsaas.boot.rest.annotations.UpdateData;
import com.nbsaas.boot.order.api.domain.request.OrderRequest;
import com.nbsaas.boot.order.api.domain.request.OrderSearch;
import com.nbsaas.boot.order.api.domain.response.OrderResponse;
import com.nbsaas.boot.order.api.domain.simple.OrderSimple;
import com.nbsaas.boot.order.api.apis.OrderApi;
import com.nbsaas.boot.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
*  对外控制器
*/
//@RequiresAuthentication
@RestController
@RequestMapping("/order")
public class OrderController {


    @Resource
    private OrderApi orderApi;



    @Resource
    private OrderExtApi orderExtApi;


//    @RequiresPermissions("order")
    @RequestMapping("/search")
    public PageResponse <OrderSimple> search(@RequestBody OrderSearch request) {
        return orderApi.search(request);
    }

    @RequiresPermissions("order")
    @RequestMapping("/list")
    public ListResponse<OrderSimple> list(@RequestBody OrderSearch request) {
        return orderApi.list(request);
    }


    @RequestMapping("/view")
    public ResponseObject <OrderResponse> view(@RequestBody @Validated(ViewOperator.class) OrderRequest  request) {
        return orderApi.view(request);
    }

    @RequestMapping("/repay")
    public ResponseObject<?> repay(@RequestBody @Validated(ViewOperator.class) OrderExtRequest request) {
        request.setCustomerId(UserUtils.user().getId());
        return orderExtApi.repay(request);
    }

    @RequestMapping("/cancel")
    public ResponseObject<?> cancel(@RequestBody @Validated(ViewOperator.class) OrderExtRequest request) {
        request.setCustomerId(UserUtils.user().getId());
        return orderExtApi.cancel(request);
    }
}